import {
  Entity,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  AfterLoad, ManyToOne, Column
} from "typeorm"
import moment from "moment/moment"
import { User } from "./User";

moment.locale('zh-cn')

export enum LikeType {
  ARTICLE = 1,
  POST = 2,
  COMMENT = 3
}

@Entity()
export class Like {
  @PrimaryGeneratedColumn()
  id: number // 点赞id

  @Column()
  type: LikeType

  @Column()
  targetId: number

  @Column()
  userId: string

  @ManyToOne(() => User, { createForeignKeyConstraints: false })
  user: User

  @CreateDateColumn()
  createdAt: Date | string

  @UpdateDateColumn()
  updatedAt: Date | string

  @AfterLoad()
  formatDate() {
    this.createdAt = moment(this.createdAt).format('LL')
    this.updatedAt = moment(this.updatedAt).format('LL')
  }
}